package org.clienttest;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.test.client.ui.util.FontAwesome;
import org.test.client.ui.util.Icons;
import org.test.client.ui.util.UIConstants;
import org.test.client.ui.util.UIUtil;

public class FontImg extends JFrame {

	private String iconHex;
	private String w;
	private String h;

	private BufferedImage img = null;
    String text="\uF02c";
    static int color=0xff000000;

	public FontImg() {
		
		JPanel jPanel = new JPanel() 
//		{
//			@Override
//			protected void paintComponent(Graphics g) {
//				super.paintComponent(g);
//				int imgw =32;
//				
//				Map<String, String> faMap = FontAwesome.getFaMap();
//				Set<Entry<String,String>> entrySet = faMap.entrySet();
//				int count = 0;
//				for(Entry e : entrySet){
//					String key = e.getKey().toString();
//					String value = e.getValue().toString();
//					
//					img = Icons.getImgByFontHex(value, imgw, new Color(count));
//					
//					int x = count % 20 *imgw;
//					int y = (count / 20 ) *imgw;
//					
//					g.drawImage(img, x, y, img.getWidth(), img.getHeight(), null);
//					
//					count ++;
//				}
//			}
//		}
		;
		
		Map<String, String> faMap = FontAwesome.getFaMap();
		Set<Entry<String,String>> entrySet = faMap.entrySet();
		int count = 0;
		for(Entry e : entrySet){
			String key = e.getKey().toString();
			String value = e.getValue().toString();
			
			img = Icons.getImgByFontHex(value, 32, new Color(count));
			JLabel jLabel = new JLabel(key+"-"+(count+1), new ImageIcon(img), JLabel.LEFT);
			
			jPanel.add(jLabel);
			count++;
		}
		System.out.println(count);
		jPanel.setPreferredSize(new Dimension(650, 3200));
		
		Container contentPane = this.getContentPane();
		contentPane.add(new JScrollPane(jPanel));

		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setSize(690, 450);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		System.out.println("");
	}

	public static BufferedImage getImgByFontHex(String iconHex, int w, int h) {

		BufferedImage img = new BufferedImage(w, h, Transparency.TRANSLUCENT);

		Graphics2D g = img.createGraphics();

		g.setFont(loadFont(Icons.dynamicFontPath,w-2));
		
		 g.setColor(new Color(color,true));

		FontMetrics fm = g.getFontMetrics();
		int stringWidth = fm.stringWidth(iconHex);
		int stringAscent = fm.getAscent();
		int stringDescent = fm.getDescent();
		int x = w / 2 - stringWidth / 2;
		int y =h / 2 + (stringAscent - stringDescent) / 2;
	        
		g.drawString(iconHex, x, y);

//		try {
//			ImageIO.write(img, "png",new File("C:/test.png"));
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
		return img;

	}

	static Font dynamicFont;
	public static Font loadFont(String fontFileName, float fontSize) // 第一个参数是外部字体名，第二个是字体大小
	{
		try {
			File file = new File(fontFileName);
			if (!file.exists()) {
				System.out.println("找不到字体文件");
			}
			if(dynamicFont == null){
				dynamicFont = Font.createFont(Font.TRUETYPE_FONT, file);
			}
//			FileInputStream aixing = new FileInputStream(file);
			Font dynamicFontPt = dynamicFont.deriveFont(fontSize);
//			aixing.close();
			return dynamicFontPt;
		} catch (Exception e)// 异常处理
		{
			http: // www.huiyi8.com/gundongdaima/html/
			e.printStackTrace();
			return new java.awt.Font("宋体", Font.PLAIN, (int) fontSize);
		}
	}
	
	public static void main(String[] args) {
		new FontImg();
	}

}

